﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Collections;
using Maticsoft.Common;
using Hyfw.BLL;
using System.Data.SqlClient;

namespace Web.Manage.Factory
{
    public partial class AjaxService : System.Web.UI.Page
    {
        FactoryManager factoryManage = new FactoryManager();
        CommonManager commonManage = new CommonManager();
        const string factory_ListProcedureName = "Factory_GetList";
        Common.UserSessionTool sessionTool = new Common.UserSessionTool();
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");

            try
            {
                BeforeInvoke(methodName);
                method.Invoke(this, null);
            }
            catch (Exception ex)
            {
                Hashtable result = new Hashtable();
                result["error"] = -1;
                result["message"] = ex.Message;
                result["stackTrace"] = ex.StackTrace;
                String json = JSON.Encode(result);
                Response.Clear();
                Response.Write(json);
            }

            finally
            {
                AfterInvoke(methodName);
            }
        }
        //权限管理
        protected void BeforeInvoke(String methodName)
        {
            //Hashtable user = GetUser();
            //if (user.role == "admin" && methodName == "remove") throw .      
        }
        //日志管理
        protected void AfterInvoke(String methodName)
        {

        }

        public void SearchEmployees()
        {
            //查询条件
            string key = Request["key"];
            //分页
            int pageIndex = Convert.ToInt32(Request["pageIndex"]);
            int pageSize = Convert.ToInt32(Request["pageSize"]);
            //字段排序
            String sortField = Request["sortField"];
            String sortOrder = Request["sortOrder"];

            //业务层：数据库操作
            Hashtable result = factoryManage.GetListBy(key, pageIndex, pageSize, sortField, sortOrder, sessionTool.getFactoryNo(), sessionTool.getRoleName(), factory_ListProcedureName);
                //commonManage.Serach(key, pageIndex, pageSize, sortField, sortOrder, factory_ListProcedureName);

            //JSON
            String json = JSON.Encode(result);
            Response.Write(json);
        }

        public void GetProvinceList()
        {
            ArrayList list = commonManage.GeListByProcedureName("S_Province_GetList");

            String json = JSON.Encode(list);
            Response.Write(json);
        }

        public void GetCityList()
        {
            int id = 0;
            id = Convert.ToInt32(Request["id"]);
            ArrayList result = commonManage.GetListByProcedureNameAndParms("S_City_GetList", new SqlParameter[] { new SqlParameter("id", id) });

            String json = JSON.Encode(result);
            Response.Write(json);
        }
        public void GetDistrictList()
        {
            int id = 0;
            id = Convert.ToInt32(Request["id"]);
            ArrayList result = commonManage.GetListByProcedureNameAndParms("S_District_GetList", new SqlParameter[] { new SqlParameter("id", id) });
            String json = JSON.Encode(result);
            Response.Write(json);
        }



        public void GetFactoryCategoryList()
        {
            String roolString = new Web.Manage.Common.UserSessionTool().getRoleName();
            ArrayList result = commonManage.GetListByProcedureNameAndParms("FactoryCategory_GetList", new SqlParameter[] { new SqlParameter("roleName", roolString) });
            String json = JSON.Encode(result);
            Response.Write(json);
        }
        //[{"factoryName":"师范生的方式","factoryCategoryId":"2","superiorFactoryNo":"100003","provnceId":"3",
        //"cityId":"3","districtId":"37","address":"撒的发生的是的","code":"","telephone":"","fax":"","email":"",]
        //"officalWebSite":"","officalOnlineStore":""}]@officalWebSite

        public void SaveFactory()
        {
            string json = Request["data"];

            ArrayList rows = (ArrayList)JSON.Decode(json);
            foreach (Hashtable row in rows)
            {
                String id = row["id"] != null ? row["id"].ToString() : "";
                //根据记录状态，进行不同的增加、删除、修改操作
                String state = row["_state"] != null ? row["_state"].ToString() : "";

                if (state == "added" || id == "")           //新增：id为空，或_state为added
                {
                   

                    if (!sessionTool.getRoleName().Equals("超级管理员"))
                    {
                        row["superiorFactoryNo"] = sessionTool.getFactoryNo();
                    }
                    else
                    {
                        row["superiorFactoryNo"] = 0;//添加经销商时候没有上游厂商
                    }
                        factoryManage.Add(row);
                }
                else if (state == "removed" || state == "deleted")
                {
                    factoryManage.Delete(int.Parse(id));
                }
                else if (state == "modified" || state == "") //更新：_state为空或modified
                {
                    factoryManage.Update(row);
                }
            }
           
        }
        public void GetFactory()
        {
            string id = Request["id"];
            Hashtable ht = (Hashtable)(factoryManage.GetFactorys(int.Parse(id))[0]);
            string json = JSON.Encode(ht);
            Response.Write(json);
        }

        public void RemoveFactorys()
        {
            string ids= Request["id"];
            factoryManage.DeleteList(ids);
        }

    }
}